<template>
  <div class="dashboard-container">
    <div class="app-container">
      <!-- 靠右的按钮 -->
      <page-tools>
        <template v-slot:after>
          <el-button type="primary" size="small" @click="openDialog('0',1)">添加权限</el-button>
        </template>
      </page-tools>
      <!-- 树形表格 -->
      <el-table border :data="list" row-key="id">
        <el-table-column align="center" prop="name" label="名称" />
        <el-table-column align="center" prop="code" label="标识" />
        <el-table-column align="center" prop="description" label="描述" />
        <el-table-column align="center" label="操作">
          <template slot-scope="{row}">
            <el-button type="text" @click="openDialog(row.id,2)">添加</el-button>
            <el-button type="text" @click="$refs['add-perm'].getSingleInfo(row.id)">编辑</el-button>
            <el-button type="text" @click="delRow(row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!-- 添加修改弹框 -->
    <AddPerm ref="add-perm" @updata="getPermissionList" />
  </div>
</template>

<script>
import { getPermissionList, delPermission } from '@/api/permisson'
import { tranListToTreeData } from '@/utils'
import AddPerm from './components/add-perm.vue'
export default {
  name: 'Permisson',
  components: {
    AddPerm
  },
  data() {
    return {
      list: []
    }
  },
  created() {
    this.getPermissionList()
  },
  methods: {
    async getPermissionList() {
      const { data } = await getPermissionList()
      // console.log(data)
      this.list = tranListToTreeData(data, '0')
      console.log(this.list)
    },
    // 添加权限
    openDialog(id, type) {
      this.$refs['add-perm'].formData.pid = id
      this.$refs['add-perm'].formData.type = type
      this.$refs['add-perm'].showDialog = true
    },
    // 删除
    async delRow(id) {
      await this.$confirm('确认删除吗？')
      await delPermission(id)
      this.$message.success('删除成功')
      this.getPermissionList()
    }
  }
}
</script>
